package tests;

import java.util.List;

import junit.framework.Assert;

import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.Diagnostician;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import statemachine.Activity;
import statemachine.ActivityTransition;
//import statemachine.Result;
import statemachine.State;
import statemachine.StateType;
import statemachine.Statemachine;
import statemachine.StatemachineFactory;

public class StatemachineTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
	}

	@Test
	public void hasAStateDefaultStateType() {
		
		State state = StatemachineFactory.eINSTANCE.createState();
		Assert.assertEquals(StateType.START, state.getType());
	}

	@Test
	public void oneStateIsNotAValidStateMachine() {
		//Missing at least another state and an activity
		State state = StatemachineFactory.eINSTANCE.createState();

		Statemachine statemachine = StatemachineFactory.eINSTANCE.createStatemachine();
		statemachine.getStates().add(state);
		
		Assert.assertTrue(!diagnose(statemachine).isEmpty()); //We need to sharpen this assert
	}

	@Test
	public void twoStatesIsNotAValidStateMachine() {
		//Missing at least an activity
		
		State stateA = StatemachineFactory.eINSTANCE.createState();
		State stateB = StatemachineFactory.eINSTANCE.createState();

		Statemachine statemachine = StatemachineFactory.eINSTANCE.createStatemachine();
		statemachine.getStates().add(stateA);
		statemachine.getStates().add(stateB);
		
		Assert.assertTrue(!diagnose(statemachine).isEmpty()); //We need to sharpen this assert
	}

	
	@Test
	public void twoStatesAndAnActivityIsNotAValidStateMachine() {
		//Missing a activity transition
		
		State stateA = StatemachineFactory.eINSTANCE.createState();
		State stateB = StatemachineFactory.eINSTANCE.createState();

		Activity activity = StatemachineFactory.eINSTANCE.createActivity();
		
		Statemachine statemachine = StatemachineFactory.eINSTANCE.createStatemachine();
		statemachine.getStates().add(stateA);
		statemachine.getStates().add(stateB);
		statemachine.getActivities().add(activity);
		
		Assert.assertTrue(!diagnose(statemachine).isEmpty()); //We need to sharpen this assert
	}


	@Test
	public void twoStatesAndAnActivityWithTransitionWithoutTriggerResultIsNotAValidStateMachine() {
		//We are missing an activity transition trigger
		State stateA = StatemachineFactory.eINSTANCE.createState();
		State stateB = StatemachineFactory.eINSTANCE.createState();

		ActivityTransition activityTransition = StatemachineFactory.eINSTANCE.createActivityTransition();
		activityTransition.setActivityToStateTransition(stateB);
		
		Activity activity = StatemachineFactory.eINSTANCE.createActivity();
		activity.getActivityTransitions().add(activityTransition);
		
		Statemachine statemachine = StatemachineFactory.eINSTANCE.createStatemachine();
		statemachine.getStates().add(stateA);
		statemachine.getStates().add(stateB);
		statemachine.getActivities().add(activity);
		
		Assert.assertTrue(!diagnose(statemachine).isEmpty()); //We need to sharpen this assert
	}
	
	
//	@Test
//	public void twoStatesAndAnActivityWithTransitionWithTriggerResultIsAValidStateMachine() {
//		State stateA = StatemachineFactory.eINSTANCE.createState();
//		State stateB = StatemachineFactory.eINSTANCE.createState();
//
//		Result result = StatemachineFactory.eINSTANCE.createResult();
//		
//		ActivityTransition activityTransition = StatemachineFactory.eINSTANCE.createActivityTransition();
//		activityTransition.setActivityToStateTransition(stateB);
//		activityTransition.setResult(result);
//		
//		Activity activity = StatemachineFactory.eINSTANCE.createActivity();
//		activity.getActivityTransitions().add(activityTransition);
//		
//		Statemachine statemachine = StatemachineFactory.eINSTANCE.createStatemachine();
//		statemachine.getStates().add(stateA);
//		statemachine.getStates().add(stateB);
//		statemachine.getActivities().add(activity);
//		
//		Assert.assertTrue(diagnose(statemachine).isEmpty());
//	}
//
//	
//	
//	
//	
	private List<Diagnostic> diagnose(EObject object) {
		Diagnostic validate = Diagnostician.INSTANCE.validate(object);
		for (Diagnostic dia : validate.getChildren()) {
			System.err.println(dia.getMessage() + ". Severity: "
					+ (dia.getSeverity() == Diagnostic.ERROR));
			

		}
		System.err.println("\n\n");
		return validate.getChildren();
	}

	
}
